/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain menu_text_color copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wanchuang.hepos.ui.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.wanchuang.hepos.R;
import com.wanchuang.hepos.app.App;
import com.wanchuang.hepos.bridge.callback.SharedViewModel;
import com.wanchuang.hepos.help.UserHelper;
import com.wanchuang.hepos.ui.LoginActivity;


/**
 * Create by KunMinX at 19/8/1
 */
public class BaseActivity extends AppCompatActivity {

    protected SharedViewModel mSharedViewModel;

    /**
     * 利用反射修改状态栏颜色
     */
    protected View statusBarView;

    private boolean isShowingTokanHint;

    private ProgressDialog loadingDialog;

    private AlertDialog mTokenHintDialog;
    private TextView loading_tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSharedViewModel = getAppViewModelProvider().get(SharedViewModel.class);
        mSharedViewModel.isLoading.observe(this, aBoolean -> {
            if (aBoolean) {
                showProgress();
            } else {
                hideProgress();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 修改状态栏
        getWindow().getDecorView().addOnLayoutChangeListener((view, i, i1, i2, i3, i4, i5, i6, i7) -> initStatusBar());
    }

    public void showLongToast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
    }

    public void showShortToast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    protected ViewModelProvider getAppViewModelProvider() {
        return ((App) getApplicationContext()).getAppViewModelProvider(this);
    }

    public void initStatusBar() {
        if (statusBarView == null) {
            //利用反射机制修改状态栏背景
            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = getWindow().findViewById(identifier);
        }
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(R.drawable.shape_title_bg);
        }

    }


    /*------------------------弹出登录失效弹窗------------------------*/
    public void showTokenInvalidHintDialog() {
        if (isShowingTokanHint) return;
        if (isFinishing()) return;
        isShowingTokanHint = true;
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("您的登录凭证已失效，请重新登录！");
        builder.setCancelable(false);
        builder.setOnDismissListener(dialog -> {
            isShowingTokanHint = false;
        });
        builder.setNegativeButton("", (dialog, which) -> {

        });
        builder.setPositiveButton("登录", (dialog, which) -> {
            UserHelper.loginOut();
            startActivity(LoginActivity.class);
            finish();
        });
        mTokenHintDialog = builder.show();
    }


    /*------------------------加载提示 loading 方法------------------------*/
    public void showProgress(String message) {
        showProgress(message, false);
    }

    public void showProgress() {
        showProgress("请稍后...", false);
    }

    public void showProgress(boolean isCancelable) {
        showProgress("请稍后...", isCancelable);
    }

    public void showProgress(String message, boolean isCancelable) {
        if (isFinishing()) return;
        if (loadingDialog == null) {
            LayoutInflater inflater = LayoutInflater.from(this);
            // 得到加载view
            View dialogLayout = inflater.inflate(R.layout.layout_view_loading, null);
            loading_tv = dialogLayout.findViewById(R.id.tv_load_dialog);
            loadingDialog = new ProgressDialog(this, R.style.transparent_dialog_theme);
            loadingDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
            loadingDialog.setCancelable(isCancelable);
            loadingDialog.setCanceledOnTouchOutside(false);
            loadingDialog.show();
            loadingDialog.setContentView(dialogLayout);
        }
        loading_tv.setText(message);
        if (getBaseContext() != null && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    public void hideProgress() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    /*------------------------页面跳转------------------------*/
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(getApplicationContext(), clz));
    }


    public boolean isDebug() {
        return getApplicationContext().getApplicationInfo() != null &&
                (getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    }

}
